/**
  ******************************************************************************
  * @file           : main.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SYS_BSP_H
#define __SYS_BSP_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32l4xx_hal.h"

#include "stm32l4xx_ll_crs.h"
#include "stm32l4xx_ll_rcc.h"
#include "stm32l4xx_ll_bus.h"
#include "stm32l4xx_ll_system.h"
#include "stm32l4xx_ll_exti.h"
#include "stm32l4xx_ll_cortex.h"
#include "stm32l4xx_ll_utils.h"
#include "stm32l4xx_ll_pwr.h"
#include "stm32l4xx_ll_dma.h"
#include "stm32l4xx_ll_rtc.h"
#include "stm32l4xx_ll_gpio.h"
#include "stm32l4xx_ll_iwdg.h"
#include "stm32l4xx_ll_usart.h"
#include "stm32l4xx_ll_lpuart.h"

#define SYS_BKP_EFFECTIVE      0x19900126u

#define DCMI_IRQn_PRIORITY              13
#define TTY_UART_IRQn_PRIORITY          15
#define WAKEUP_KEY2_IRQn_PRIORITY       14

/* Private defines -----------------------------------------------------------*/
#define LED3_Pin                GPIO_PIN_14
#define LED3_LL_Pin             LL_GPIO_PIN_14
#define LED3_GPIO_Port          GPIOB
#define LED3(x)                 x?(LED3_GPIO_Port->BSRR = (uint32_t)LED3_Pin):(LED3_GPIO_Port->BRR = (uint32_t)LED3_Pin)
#define LED3_Toggle()           HAL_GPIO_TogglePin(LED3_GPIO_Port, LED3_Pin)

#define LED1_Pin                GPIO_PIN_5
#define LED1_LL_Pin             LL_GPIO_PIN_5
#define LED1_GPIO_Port          GPIOA
#define LED1(x)                 x?(LED1_GPIO_Port->BSRR = (uint32_t)LED1_Pin):(LED1_GPIO_Port->BRR = (uint32_t)LED1_Pin)
#define LED1_Toggle()           HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin)

//#define LED2_Pin                GPIO_PIN_6
//#define LED2_LL_Pin             LL_GPIO_PIN_6
//#define LED2_GPIO_Port          GPIOB
//#define LED2(x)                 x?(LED2_GPIO_Port->BSRR = (uint32_t)LED2_Pin):(LED2_GPIO_Port->BRR = (uint32_t)LED2_Pin)
//#define LED2_Toggle()           HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin)

#define WAKEUP_KEY2_Pin         GPIO_PIN_13
#define WAKEUP_KEY2_LL_Pin      LL_GPIO_PIN_13
#define WAKEUP_KEY2_GPIO_Port   GPIOC
#define WAKEUP_KEY2_EXTI_LINE   LL_EXTI_LINE_13
#define WAKEUP_KEY2_EXTI_IRQn   EXTI15_10_IRQn
#define WakeUpKey2()            (WAKEUP_KEY2_GPIO_Port->IDR & WAKEUP_KEY2_Pin)


typedef struct
{
	volatile uint8_t  year;  /* 0~99 */
	volatile uint8_t  month;
	volatile uint8_t  day;
	volatile uint8_t  hour;
	volatile uint8_t  minute;
	volatile uint8_t  second;
  volatile uint8_t  newmin;
  volatile uint16_t allmin;
}Sys_DataTimeType;

extern Sys_DataTimeType Sys_Runtime;
extern Sys_DataTimeType Sys_Adjtime;

extern void Tty_Sendbuf(const uint8_t *buf, uint32_t len);

extern void SysHal_Init(void);
extern void Sys_GetDataTime(void);
extern void Sys_SetDataTime(void);

/* debug macro */
#if (SYS_DEBUG_LEVEL > 0)
#define  SysInfo(...)    _printf(__VA_ARGS__);

#else
#define SysInfo(...)   
#endif 
                            
#if (SYS_DEBUG_LEVEL > 1)                         
#define  SysDbgLog(...)    _printf(__VA_ARGS__);//_printf("[D]:") ;
                           
#else
#define SysDbgLog(...)                         
#endif

#ifdef SYS_ERRLOG
#define  SysErrLog(...)    _printf("[E] '%s' '%s' '%d':\r\n", __FILE__, __FUNCTION__, __LINE__) ;\
                           _printf(__VA_ARGS__);
#else
#define SysErrLog(...)   
#endif 

#ifdef __cplusplus
}
#endif

#endif /* __SYS_BSP_H */
